# !/usr/bin/env python
# -*- coding: utf-8 -*-
# ///////////////////////////////////////////////////////////////
# @File    :   getImageBaseData.py
# @Time    :   2024/08/21 14:39:56
# @Author  :   kai.luo
# @Version :   1.0.0
# @Desc    :   识别文字图片中,输出获取解析图
# ///////////////////////////////////////////////////////////////
from paddleocr import PaddleOCR, draw_ocr  # type: ignore
import os
from PIL import Image


class DiscernImageResult(object):
    def __init__(self, **kwarg) -> None:
        # 补位参数
        self.space_5 = [' ']*5
        self.space_2 = [' ']*2

        # 初始化参数
        self.header_data_list = list()

    def createParserImage(self, img_path, save_folder):
        """
        生成可视化图
        Args:
            result (_type_): _description_
            img_path (_type_): _description_
            save_folder (_type_): _description_
        """
        ocr = PaddleOCR(use_angle_cls=True, lang="ch")  # need to run only once to download and load model into memory
        result = ocr.ocr(img_path, cls=True)
        for idx in range(len(result)):
            res = result[idx]
            for line in res:
                print(line)
        result = result[0]
        image = Image.open(img_path).convert('RGB')
        boxes = [line[0] for line in result]
        txts = [line[1][0] for line in result]
        scores = [line[1][1] for line in result]
        im_show = draw_ocr(image, boxes, txts, scores, font_path='/home/duhf/Downloads/ppocr_img/fonts/simfang.ttf')
        im_show = Image.fromarray(im_show)
        im_show.save(os.path.join(save_folder, 'result.jpg'))
